﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class DepartmentService : IEntityService<PhongBan>
    {
        public List<PhongBan> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.PhongBans.ToList();
            }
        }

        public bool Add(PhongBan pb)
        {
            using (HRMEntities enties = new HRMEntities())
            {
                enties.PhongBans.Add(pb);
                enties.SaveChanges();
                return true;
            }
        }

        public bool Update(PhongBan pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = (from d in entities.PhongBans where d.MaPhongBan == pb.MaPhongBan select d).FirstOrDefault();
                if (query != null)
                {
                    query.SDT = pb.SDT;
                    query.TenPhongBan = pb.TenPhongBan;
                    query.DiaChi = pb.DiaChi;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(PhongBan pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = (from d in entities.PhongBans where d.MaPhongBan == pb.MaPhongBan select d).FirstOrDefault();
                if (query != null)
                {
                    entities.PhongBans.Remove(query);
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool DeleteList(List<PhongBan> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in list)
                {
                    var de = entities.PhongBans.Where(x => x.MaPhongBan == item.MaPhongBan).First();
                    entities.PhongBans.Remove(de);
                }
                entities.SaveChanges();
                return true;
            }
        }

        public List<PhongBan> GetPaging(int pageIndex, int pageSize)
        {
            var list = new List<PhongBan>();

            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.PhongBans.OrderBy(p => p.MaPhongBan).Skip((pageIndex - 1) * pageSize).Take(pageSize);

                foreach (var item in query)
                {
                    list.Add(item);
                }
            }

            return list;
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.PhongBans.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }
    }
}
